/**
 *  版权所有 Copyright@2016-04-05 12:06:56
 */
package com.fairy.sso.jedis.sharding;

import java.util.ArrayList;
import java.util.List;

import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;

import com.alibaba.fastjson.JSON;
import com.fairy.sso.config.FairyConfig;

import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisShardInfo;
import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;


/**   
 * <p>
 * Sharding Redis Client 工具类
 * </p>
 * @Title: PerUserEntity
 * @Description: 个人用户
 * @author yyk1504@163.com
 * @date 2016-04-05 16:13:56
 * @version V1.0   
 *
 */
public class ShardingRedisClient {
	//@Autowired(required=true)
	//private  FairyConfig fairyConfig;
	WebApplicationContext wac = ContextLoader.getCurrentWebApplicationContext();
	FairyConfig fairyConfig=(FairyConfig) wac.getBean("fairyConfig");
	private  ShardedJedisPool shardedJedisPool;

	public ShardingRedisClient()
	{
		// 读取相关的配置
		//ResourceBundle resourceBundle = ResourceBundle.getBundle("redis");
		int maxActive = Integer.valueOf(fairyConfig.getMaxActive());//Integer.parseInt(resourceBundle.getString("redis.pool.maxActive"));
		int maxIdle =Integer.valueOf(fairyConfig.getMaxIdle());//Integer.parseInt(resourceBundle.getString("redis.pool.maxIdle"));
		int maxWait = Integer.valueOf(fairyConfig.getMaxWait());//Integer.parseInt(resourceBundle.getString("redis.pool.maxWait"));

		//String ip = resourceBundle.getString("redis.ip");
		//int port = Integer.parseInt(resourceBundle.getString("redis.port"));
		
		//设置配置
		JedisPoolConfig config = new JedisPoolConfig();
		config.setMaxTotal(maxActive);
		config.setMaxIdle(maxIdle);
		config.setMaxWaitMillis(maxWait);
		
		//设置分片元素信息
		/**
		JedisShardInfo shardInfo1 = new JedisShardInfo(ip,port);
		JedisShardInfo shardInfo2 = new JedisShardInfo(ip,port);
		List<JedisShardInfo> list = new ArrayList<JedisShardInfo>();
		list.add(shardInfo1);
		list.add(shardInfo2);*/
		
		
		List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>();
		String redisIp=fairyConfig.getAddress();
		 if(redisIp.contains(","))
		 {
			 String[]arr=redisIp.split(",");
			 for(String ips:arr)
			 {
				 String[]array=ips.split(":");
				 String ip=array[0];
				 int port=Integer.valueOf(array[1]);
				 shards.add(new JedisShardInfo(ip, port));
			 }
		 }
		 else
		 {
			 String[]array=redisIp.split(":");
			 String ip=array[0];
			 int port=Integer.valueOf(array[1]);
			 shards.add(new JedisShardInfo(ip, port));
		 }
		shardedJedisPool = new ShardedJedisPool(config, shards);
	}
	
	/**
	 * 向缓存中设置字符串内容
	 * @param key key
	 * @param value value
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("deprecation")
	public  boolean  set(String key,String value) throws Exception{
		ShardedJedis jedis = null;
		try {
			jedis = shardedJedisPool.getResource();
			jedis.set(key, value);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}finally{
			shardedJedisPool.returnResource(jedis);
		}
	}
	
	/**
	 * 向缓存中设置对象
	 * @param key 
	 * @param value
	 * @return
	 */
	@SuppressWarnings("deprecation")
	public  boolean  set(String key,Object value){
		ShardedJedis jedis = null;
		try {
			String objectJson = JSON.toJSONString(value);
			jedis = shardedJedisPool.getResource();
			jedis.set(key, objectJson);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}finally{
			shardedJedisPool.returnResource(jedis);
		}
	}
	
	/**
	 * 删除缓存中得对象，根据key
	 * @param key
	 * @return
	 */
	@SuppressWarnings("deprecation")
	public  boolean del(String key){
		ShardedJedis jedis = null;
		try {
			jedis = shardedJedisPool.getResource();
			jedis.del(key);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}finally{
			shardedJedisPool.returnResource(jedis);
		}
	}
	
	/**
	 * 根据key 获取内容
	 * @param key
	 * @return
	 */
	@SuppressWarnings("deprecation")
	public  String get(String key){
		ShardedJedis jedis = null;
		String value="";
		try {
			jedis = shardedJedisPool.getResource();
			//Object value = jedis.get(key);
			value = jedis.get(key);
			//return value;
		} catch (Exception e) {
			e.printStackTrace();
			//return false;
		}finally{
			shardedJedisPool.returnResource(jedis);
		}
		return value;
	}
	

	/**
	 * 根据key 获取对象
	 * @param key
	 * @return
	 */
	@SuppressWarnings("deprecation")
	public  <T> T get(String key,Class<T> clazz){
		ShardedJedis jedis = null;
		try {
			jedis = shardedJedisPool.getResource();
			String value = jedis.get(key);
			return JSON.parseObject(value, clazz);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}finally{
			shardedJedisPool.returnResource(jedis);
		}
	}
	
}
